package pl.edu.mimuw.irs.core.model.design.resource;

import java.util.Set;


/**
 * @author Jakub Rauch
 * Created on: 2009-05-07
 */
public class Resource extends AbstractRolesHolder implements IMember {

	private static final long serialVersionUID = -6786426056173104308L;
	
	// *** Attributes

	private int unitCost;
	private Resources resources;
	// Getters and setters
	public int getUnitCost() {
		return unitCost;
	}
	public void setUnitCost(int unitCost) {
		this.unitCost = unitCost;
	}
	public Resources getResources() {
		return resources;
	}
	@SuppressWarnings("unchecked")
	public IHolder getHolder() {
		return getResources();
	}
	public void setResources(Resources resources) {
		if (this.resources == resources) return;
		Resources oldParent = null;
		if (this.resources != null) oldParent = this.resources;
		this.resources = resources;
		if (oldParent != null) oldParent.remove(this);
	}
	
	// *** Constructors
	
	public Resource(String name, Set<RoleInstance> rolesInstances) {
		super(name, rolesInstances);
		setUnitCost(10);
	}
	
	public Resource(Set<RoleInstance> rolesInstances) {
		this(null, rolesInstances);
	}
	
	public Resource() {
		this(null);
	}
	
	// *** Methods
	
	/**
	 * Actor meets requirement if he is capable of playing every role of this requirement.
	 */
	public boolean meets(Requirement requirement) {
		return getRolesMap().keySet().containsAll(requirement.getRolesMap().keySet());
	}
	
	/**
	 * Works on requirement. Returns time taken - depends on cost of requirement and 
	 * skills of this actor. Assumes this actor meets this requirement.
	 * @param requirement
	 * @return
	 */
	public double work(Requirement requirement) {
		double timeUnits = 0;
		for (RoleInstance roleInstance : requirement.getRolesMap().values()) {
			timeUnits = roleInstance.getModifier();
		}
		
		return timeUnits;
	}
	
	@Override
	protected String getNamePrefix() {
		return "Res";
	}
	
	public IMember copy() {
		Resource resource = new Resource();
		for (RoleInstance roleInstance : this.getRolesMap().values()) {
			resource.add(roleInstance.copy());
		}
		return resource;
	}

	public void delete() {
		getResources().remove(this);
	}
}
